//
//  NextViewController.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/12.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "NextViewController.h"
#import "NextCollectionViewCell.h"
#import "GetData.h"
#import "NextModel.h"
#import "SegViewController.h"
@interface NextViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, strong)NextModel *model;
@property(nonatomic, copy)NSString *url;
@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setHidesBackButton:TRUE animated:NO];
    [self getData];
    self.downView = [[UIView alloc]initWithFrame:CGRectMake(-1, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width + 2, 51)];
    _downView.backgroundColor = [UIColor blackColor];
    _downView.alpha = 1;
    _downView.layer.borderWidth = 1;
    _downView.layer.borderColor =[[UIColor grayColor ]CGColor];;
    
    [self.view addSubview:_downView];
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(10, 15, 24, 24);
    
    [self.backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [self.downView addSubview:_backButton];

    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(90, 90);
    layout.minimumInteritemSpacing = 0;
    self.collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - 44) collectionViewLayout:layout];
    self.collectionview.backgroundColor = [UIColor whiteColor];
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    [self.collectionview registerClass:[NextCollectionViewCell class] forCellWithReuseIdentifier:@"Next"];
    
    [self.view addSubview:_collectionview];
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];
    
    
    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(64 * 2, 568 - 64, 64, 64)];
    [self.view addSubview:black];
    black.layer.cornerRadius = 32;
    black.clipsToBounds = YES;
    
    black.backgroundColor = [UIColor blackColor];
    
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.play.frame = CGRectMake(0, 0, 60, 60);
    self.play.center = black.center;
    
    [self.view addSubview:self.play];
    
    self.play.layer.cornerRadius = 30;
    [self.view bringSubviewToFront:self.play];
    self.play.clipsToBounds = YES;
    [self.play addTarget:self action:@selector(GotoPlay) forControlEvents:UIControlEventTouchUpInside];
    
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    // Do any additional setup after loading the view.
}
-(void)getData
{
    _url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_tag_list?category=%@&device=iPhone&type=album",self.name];
    
    GetData *data = [[GetData alloc]init];
    [data getDataURL:_url dic:nil block:^(id result) {
        _model = [[NextModel alloc]init];
        [_model setValuesForKeysWithDictionary:result];

       
        [_collectionview reloadData];
        [self.mbview removeFromSuperview];

    }];
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _model.list.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Next" forIndexPath:indexPath];
    
    [cell setModel:_model num:indexPath.item];
    
    
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SegViewController *vc = [[SegViewController alloc]init];
    vc.name = [_model.list[indexPath.item] objectForKey:@"tname"];
    vc.url = _name;
    vc.title =[_model.list[indexPath.item] objectForKey:@"tname"];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


-(void)viewDidDisappear:(BOOL)animated
{
    [self.Timer invalidate];
    _Timer =nil;
    
}
-(void)viewWillAppear:(BOOL)animated
{
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    _Playmodel = audioManager.Mymodel;
    
    
    if (audioManager.flag) {
        
        if (!_Timer) {
            //            self.Timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
            
            self.Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
        }
        
        //将定时器加入循环
        //循环用于系统页面响应,定时器和进程
        //                [[NSRunLoop currentRunLoop] addTimer:_Timer forMode:NSDefaultRunLoopMode];
        
        
        [_Timer setFireDate:[NSDate distantPast]];
        
        
        
        
    }
    else
    {
        
        [_Timer setFireDate:[NSDate distantFuture]];
        
        //        if ([_Timer isValid]) {
        //            永久停止
        //            [self.Timer invalidate];
        //            _Timer =nil;
        //        }
        
        
    }
    
}
-(void)zhuan
{
    self.play.transform = CGAffineTransformRotate(self.play.transform,2 * M_PI / 60.0 /100);
    //    if (_model.coverLarge) {
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    //    }
}

-(void)GotoPlay
{
    PlayViewController *vc = [[PlayViewController alloc]init];
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    vc.model = audioManager.Mymodel;
    vc.DataSource = audioManager.Mydata;
    /**
     *  number不记录
     */
    
    //    vc.number = audioManager.num;
    vc.num = [NSString stringWithFormat:@"%ld",audioManager.num];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
